var path = require("path");   //引入nodejs的path模块
const glob = require("glob")
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin'); 
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');
const PurifycssWebpack = require("purifycss-webpack");  //删除未使用的css



module.exports = (env,argv) => {
    let devMode = true;
    if(argv.mode === "production"){
        devMode = false
    }
    var config = {
        // devtool: 'inline-source-map',
        entry:{
            index:"./src/js/index.js",
            login:"./src/js/login.js"
        },
        output:{
            filename:'./js/[name].js',
            chunkFilename:"./js/[name].js",
            path:path.resolve(__dirname,'dist')
        },
        devServer:{
            contentBase:'./dist',
            host:'localhost',
            compress:true,
            port:8080,
            hot:true,   //热更新对html修改无效
            open:true
        },
        module:{
            rules:[ 
                {
                    test:/\.js$/,
                    exclude:/node_modules/,
                    loader:'babel-loader',
                    options:{
                        presets:["env"]
                    },
                    include:/src/,
                },
                {
                    test: /\.css$/,
                    // style-loader 会把样式打到js中
                    use: [devMode?"style-loader":MiniCssExtractPlugin.loader,"css-loader","postcss-loader"]
                },
                {
                    test: /\.(png|svg|jpg|gif|jpeg)$/,
                    loader: 'url-loader',
                    options:{
                        limit: 8192,
                        outputPath: "images/",
                        name:'[name].[ext]'
                    }
                },
                {
                    test: /\.(woff|woff2|eot|ttf|otf)$/,
                    loader: 'file-loader',
                    options: {
                        outputPath:'fonts/',
                        name: '[name].[ext]'
                    }
                },
                {
                    test: /\.(htm|html)$/,
                    use: ['html-withimg-loader']
                }
            ]
        },
        plugins: [
            new CleanWebpackPlugin('dist'),
            new CopyWebpackPlugin([
                {from:'./src/assets',to:'./dist/assets'}
            ]),
            new HtmlWebpackPlugin({
                template:"./src/index.html",
                filename:"index.html",
                hash:true,
                chunks:["index","vendor","utils"]
            }),	// 配置plugin
            new HtmlWebpackPlugin({
                template:"./src/login.html",
                filename:"login.html",
                hash:true,
                hot:true,
                chunks:["login","vendor","utils"]
            }),	// 配置plugin
            new PurifycssWebpack({
                paths : glob.sync(path.resolve("src/*.html"))
            }),
            new MiniCssExtractPlugin({
                filename: "css/[name].css",
                chunkFilename: "css/[id].css"
              }),
            new webpack.ProvidePlugin({
                $: "jquery",
                jQuery:"jquery"
            }),                                              
            new webpack.HotModuleReplacementPlugin() 
        ],
        optimization: {
            splitChunks: {
                cacheGroups: {
                    vendor: {   // 抽离第三方插件
                        test: /node_modules/,   // 指定是node_modules下的第三方包
                        chunks: 'initial',
                        name: 'vendor',  // 打包后的文件名，任意命名    
                        // 设置优先级，防止和自定义的公共代码提取时被覆盖，不进行打包
                        priority: 10    
                    },
                    utils: { // 抽离自己写的公共代码，utils这个名字可以随意起
                        chunks: 'initial',
                        name: 'utils',  // 任意命名
                        minSize: 1024    // 只要超出0字节就生成一个新包
                    }
                }
            }
        }
    }
    return config
}